using System;
using System.IO;
using VBFileIO;

//using System.Windows.Forms;
//using VBFileIO;

public class FileCtrl
{
    public static void FolderCopy(string strFolderFrom, string strFolderTo, bool bRecursiveFlg)
    {
        FileCopy(strFolderFrom, strFolderTo, bRecursiveFlg);
    }

    public static int get_file_number(DirectoryInfo diRoot, ref int iDirectories)
    {
        DirectoryInfo[] dirs = diRoot.GetDirectories();
        foreach (DirectoryInfo dir in dirs)
        {
            iDirectories++;
            get_file_number(dir, ref iDirectories);
        }

        FileInfo[] theFiles = diRoot.GetFiles();
        foreach (FileInfo theFile in theFiles)
        {
            iDirectories++;
        }
        return iDirectories;
    }

    public static void CreateDirectory(string DirName)
    {
        if (!Directory.Exists(DirName))
        {
            Directory.CreateDirectory(DirName);
        }
    }

    public static void FileAccess(string fileName)
    {
        File.SetAttributes(fileName, FileAttributes.Normal);
    }

    public static void FileCopy(string strFolderFrom, string strFolderTo, bool bRecursiveFlg)
    {
        DirectoryInfo dir;
        FileInfo[] files;
        DirectoryInfo[] dirs;
        string strTmpPath;

        if (!Directory.Exists(strFolderTo))
        {
            Directory.CreateDirectory(strFolderTo);
        }

        dir = new DirectoryInfo(strFolderFrom);

        if (!dir.Exists)
        {
            //throw new UserDefinedException("e_001", strFolderFrom);
        }

        files = dir.GetFiles();

        foreach (FileInfo file in files)
        {
            strTmpPath = Path.Combine(strFolderTo, file.Name);

            try { file.CopyTo(strTmpPath, true); }
            catch (UnauthorizedAccessException)
            {
                if ((FileAttributes.ReadOnly & File.GetAttributes(strTmpPath)) != 0)
                {
                    File.SetAttributes(strTmpPath, FileAttributes.Normal);
                    file.CopyTo(strTmpPath, true);
                    File.SetAttributes(strTmpPath, FileAttributes.ReadOnly);
                }
            }
        }

        files = null;

        if (!bRecursiveFlg)
        {
            return;
        }

        dirs = dir.GetDirectories();

        foreach (DirectoryInfo subDir in dirs)
        {
            strTmpPath = Path.Combine(strFolderTo, subDir.Name);
            FileCopy(subDir.FullName, strTmpPath, bRecursiveFlg);
        }

        dirs = null;
        dir = null;
    }

    public static void DirectoryDelete(string path)
    {
        if (File.Exists(path))
        {
            try
            {
                File.Delete(path);
            }
            catch (System.UnauthorizedAccessException)
            {
                File.SetAttributes(path, FileAttributes.Normal);
                File.Delete(path);
            }
        }

        if (Directory.Exists(path))
        {
            try
            {
                Directory.Delete(path, true);
            }
            catch (System.UnauthorizedAccessException)
            {
                DirectoryInfo current = new DirectoryInfo(path);

                DirectoryInfo[] dirs = current.GetDirectories();
                FileInfo[] files = current.GetFiles();

                foreach (FileInfo file in files) { DirectoryDelete(file.FullName); }

                foreach (DirectoryInfo dir in dirs)
                {
                    DirectoryDelete(dir.FullName);
                }

                DirectoryDelete(path);
            }
        }
    }

    public static void PutRececal(string filename)
    {
        //FileInfo fi = new FileInfo(strFileName);

        //DriveInfo driver = new DriveInfo(fi.Directory.Root.Name);

        //DirectoryInfo directoryInfo = new DirectoryInfo(Path.Combine(driver.Name , "\\Recycled"));

        //File.Move(fi.FullName,directoryInfo.FullName);

        if (File.Exists(filename))
        {
            try
            {
                Recycler.DelFile(filename);
            }
            catch (Exception ex)
            {
                Message.SendMsg(ex.Message);
            }
        }
        else if (Directory.Exists(filename))
        {
            try
            {
                Recycler.DelDir(filename);
            }
            catch (Exception ex)
            { Message.SendMsg(ex.Message); }
        }
        else
        {
            Message.SendMsg("file not exit");
        }
    }
}